<?php
function func_crc32($str)
{
    $crc32 = crc32($str);

    if (crc32('test') != -662733300 && $crc32 > 2147483647)
        $crc32 -= 4294967296;

    $hex = dechex(abs($crc32));

    return str_repeat('0', 8-strlen($hex)).$hex;
}
function text_crypt($s, $type = 'B', $key = false)
{
    global $encryption_types;

    if (strlen($s) == 0)
        return $s;

    if (!in_array((string)$type, $encryption_types))
        $type = 'B';

    $s = trim($s);
    $s .= func_crc32(md5($s));

	$key = 'd4637b3c460f4528867761511222145c';
    $s = func_bf_crypt($s, $key);

    return $type."-".$s;
}
function func_array_merge()
{

    $vars = func_get_args();

    $result = array();

    if (
        !is_array($vars)
        || empty($vars)
    ) {

        return $result;

    }

    foreach($vars as $v) {

        if (
            is_array($v)
            && !empty($v)
        ) {

            $result = array_merge($result, $v);

        }

    }

    return $result;
}

function func_array_map($func, $var)
{

    if (!is_array($var)) return $var;

    foreach($var as $k => $v)
        $var[$k] = call_user_func($func, $v);

    return $var;
}

function func_array2insert ($tbl, $arr, $is_replace = false, $is_ignore = false)
{
    global $sql_tbl;

    if (empty($tbl) || empty($arr) || !is_array($arr))
        return false;

    if (!empty($sql_tbl[$tbl]))
        $tbl = $sql_tbl[$tbl];

    $query = $is_replace ? 'REPLACE' : ('INSERT' . ($is_ignore ? ' IGNORE' : ''));

    $arr_keys = array_keys($arr);

    // func_check_tbl_fields($tbl, $arr_keys);

    foreach ($arr_keys as $k => $v) {

        if (!preg_match('/^`.*`$/Si', $v, $out)) {

            $arr_keys[$k] = "`" . $v . "`";

        }

    }

    $arr_values = array_values($arr);

    foreach ($arr_values as $k => $v) {

        if (!preg_match('/^["\'].*["|\']$/Si', $v, $out)) {

            $arr_values[$k] = "'" . $v . "'";

        }

    }

    $query .= ' INTO ' . $tbl . ' (' . implode(', ', $arr_keys) . ') VALUES (' . implode(', ', $arr_values) . ')';

    $r = db_query($query);

    

    return $r;
}

